<template>
  <div class="setting-container">
    <div class="app-container">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="角色设置" name="role">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="show">新增角色</el-button>
          <el-table v-loading="loading" :data="tableData" style="width:100%; margin:30px" border stripe>
            <el-table-column type="index" :index="indexMethod" label="序号" width="50px" />
            <el-table-column width="250px" label="角色名称" prop="name" />
            <el-table-column width="250px" label="角色描述" prop="description" />
            <el-table-column width="400px" label="操作">
              <template #default="{row}">
                <el-button size="small" type="success">分配权限-后续完成</el-button>
                <el-button size="small" type="primary" @click="edit(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
              :current-page="query.page"
              :page-sizes="[1, 2, 3, 4]"
              :page-size="query.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
          <el-dialog
            title="新增角色"
            :visible="dialogVisible"
            @close="close"
          >
            <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="ruleForm.name" type="text" autocomplete="off" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="ruleForm.description" type="text" autocomplete="off" />
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="close">取 消</el-button>
              <el-button type="primary" @click="add">确 定</el-button>
            </span>
          </el-dialog>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="company">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
          />
          <el-form ref="infoRef" v-loading="loading" :model="infoForm" label-width="100px" class="demo-ruleForm" style="margin-top:20px">
            <el-form-item label="公司名称" prop="name">
              <el-input v-model="infoForm.name" type="text" disabled />
            </el-form-item>
            <el-form-item label="公司地址" prop="companyAddress">
              <el-input v-model="infoForm.companyAddress" type="text" disabled />
            </el-form-item>
            <el-form-item label="邮箱" prop="mailbox">
              <el-input v-model="infoForm.mailbox" type="text" disabled />
            </el-form-item>
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="infoForm.remarks" type="text" disabled />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqAddSetting, reqDelSetting, reqGetSettingDetail, reqGetSettingList, reqUpdateSetting } from '@/api/setting'
import { reqGetCompanyInfo } from '@/api/company'
import { mapState } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      infoForm: {
      },
      loading: false, // 加载显示
      dialogVisible: false, // 对话框显示
      ruleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: ['blur', 'change'] }
        ]
      },
      tableData: [], // 表格数据
      activeName: 'role',
      query: {
        page: 1, // 当前页码
        pagesize: 2 // 每页条数
      },
      total: 0 // 所有条数
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.getSettingList()
  },
  methods: {
    async getSettingList() {
      this.loading = true
      const { data } = await reqGetSettingList(this.query.page, this.query.pagesize)
      this.total = data.total
      this.tableData = data.rows
      this.loading = false
    },
    // 标签页切换触发事件
    async handleClick(tab, event) {
      if (tab.name === 'company') {
        this.loading = true
        const { data } = await reqGetCompanyInfo(this.userInfo.companyId)
        this.infoForm = data
        this.loading = false
      }
    },
    handleSizeChange(val) {
      this.query.pagesize = val
      this.getSettingList()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getSettingList()
    },
    // 序号切换
    indexMethod(index) {
      return index + 1 + (this.query.page - 1) * this.query.pagesize
    },
    // 显示对话框
    show() {
      this.dialogVisible = true
    },
    close() {
      this.dialogVisible = false
      this.$refs.formRef.resetFields()
      this.ruleForm = {
        id: '',
        name: '',
        description: ''
      }
    },
    // 添加
    add() {
      this.$refs.formRef.validate(async boo => {
        if (!boo) return
        if (this.ruleForm.id) {
          await reqUpdateSetting(this.ruleForm)
        } else {
          await reqAddSetting(this.ruleForm)
        }
        this.close()
        this.getSettingList()
      })
    },
    // 删除
    async  del(id) {
      const res = await this.$confirm('您确定要删除当前角色吗', '删除提示', {
        type: 'warning'
      }).catch(() => {})
      if (res !== 'confirm') return this.$message.info('已取消操作')
      await reqDelSetting(id)
      if (this.tableData.length === 1 && this.query.page > 1) this.query.page--
      this.getSettingList()
    },
    // 获取角色详情
    async edit(id) {
      const { data } = await reqGetSettingDetail(id)
      this.ruleForm = data
      this.show()
    }
  }
}
</script>

<style>

</style>

